package com.goldgov.statistics.indexchar.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.conference.service.ConferenceStat;
import com.goldgov.conference.service.ConferenceStatQuery;
import com.goldgov.conference.service.ConferenceStatService;
import com.goldgov.kcloud.core.json.JsonListObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.learnmanage.service.User;
import com.goldgov.learnmanage.service.UserQuery;
import com.goldgov.learnmanage.service.UserService;
import com.goldgov.organization.service.Organization;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/workbench/statistics/indexchart")
@Api("数据统计首页接口")
public class IndexCharController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ConferenceStatService conferenceStatService;
	
	@GetMapping("/listUserByGender")
	@ApiOperation(value = "机构下各性别人数", notes = "机构下各性别人数")
	public JsonQueryObject<User> listUserByGender( UserQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if (PropertyUtil.objectNotEmpty(query.getSearchOrgID())) {
			List<User> list=userService.listUserByGender(query.getSearchOrgID());
			query.setResultList(list);
		}
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/listOrgByType")
	@ApiOperation(value = "机构下各类型机构数量", notes = "机构下各类型机构数量")
	public JsonListObject<Organization> listOrgByType( UserQuery query,HttpServletRequest request,HttpServletResponse response) throws Exception {
		//1党委，2总支，3支部
		List<Organization> list=new ArrayList<Organization>();
		if (PropertyUtil.objectNotEmpty(query.getSearchOrgID())) {
			 list=userService.listOrgByType(query.getSearchOrgID());
		}
		return new JsonListObject<Organization>(list);
	}
	
	
	@GetMapping("/countConferencePercentage")
	@ApiOperation(value = "会议完成百分比", notes = "各类型会议完成百分比")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchType", value = "会议类型", paramType = "query"),
			@ApiImplicitParam(name = "searchYear", value = "年份", paramType = "query"),
			@ApiImplicitParam(name = "searchQuarter", value = "季", paramType = "query"),
			@ApiImplicitParam(name = "searchMonth", value = "月", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "query"),
			@ApiImplicitParam(name = "serarchNextNum", value = "子机构数", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationType", value = "机构类型", paramType = "query")})
	public JsonQueryObject<ConferenceStat> countConferencePercentage(@ApiIgnore ConferenceStatQuery query,
			HttpServletRequest request, Model model) {
		if (query.getSearchYear() == null || "".equals(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		if(ConferenceStatQuery.DK.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_DK);
		}else if(ConferenceStatQuery.DXZH.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_DXZH);
		}else if(ConferenceStatQuery.ZBDYDH.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_ZBDYDH);
		}else if(ConferenceStatQuery.ZBWYHHY.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_ZBWYHHY);
		}
		List<ConferenceStat> list=conferenceStatService.countConferencePercentage(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/countConferenceAttachPercentage")
	@ApiOperation(value = "会议材料分布百分比", notes = "会议材料分布百分比")
	@ApiImplicitParams({@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
		@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "query")})
	public JsonQueryObject<ConferenceStat> countConferenceAttachPercentage(@ApiIgnore ConferenceStatQuery query,
			HttpServletRequest request, Model model) {
		List<ConferenceStat> list=conferenceStatService.countConferenceAttachPercentage(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/countConferencePercentageAvg")
	@ApiOperation(value = "会议指标完成度", notes = "各类型会议指标完成度")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchType", value = "会议类型", paramType = "query"),
			@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
			@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "query"),
			@ApiImplicitParam(name = "serarchNextNum", value = "子机构数", paramType = "query"),
			@ApiImplicitParam(name = "searchOrderType", value = "排序类型", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationType", value = "机构类型", paramType = "query")})
	public JsonQueryObject<ConferenceStat> countConferencePercentageAvg(@ApiIgnore ConferenceStatQuery query,
			HttpServletRequest request, Model model) throws Exception {
		if (!PropertyUtil.objectNotEmpty(query.getSearchBeginDate())) {
			//取得当前年
			int year=DateUtil.getYear(new Date());
			query.setSearchBeginDate(year+"-01");
			query.setSearchEndDate(year+"-12");
		}
		
		/*if(ConferenceStatQuery.DK.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_DK);
		}else if(ConferenceStatQuery.DXZH.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_DXZH);
		}else if(ConferenceStatQuery.ZBDYDH.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_ZBDYDH);
		}else if(ConferenceStatQuery.ZBWYHHY.equals(Integer.valueOf(query.getSearchType()))) {
			query.setSearchReachConferenceNum(ConferenceStatQuery.REACH_NUM_ZBWYHHY);
		}*/
		List<ConferenceStat> list=conferenceStatService.countConferencePercentageAvg(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/countConferencePercentageRegion")
	@ApiOperation(value = "各地区会议指标完成度", notes = "会议总体指标完成度")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchType", value = "会议类型", paramType = "query"),
			@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
			@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "query"),
			@ApiImplicitParam(name = "serarchNextNum", value = "子机构数", paramType = "query"),
			@ApiImplicitParam(name = "searchOrderType", value = "排序类型", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationType", value = "机构类型", paramType = "query")})
	public JsonQueryObject<ConferenceStat> countConferencePercentageRegion(@ApiIgnore ConferenceStatQuery query,
			HttpServletRequest request, Model model) throws Exception {
		if (!PropertyUtil.objectNotEmpty(query.getSearchBeginDate())) {
			//取得当前年
			int year=DateUtil.getYear(new Date());
			query.setSearchBeginDate(year+"-01");
			query.setSearchEndDate(year+"-12");
		}
		List<ConferenceStat> list=conferenceStatService.countConferencePercentageRegion(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/countConferencePercentageBynum")
	@ApiOperation(value = "各类型会议完成数量对比", notes = "各类型会议完成数量对比")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchType", value = "会议类型", paramType = "query"),
			@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
			@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "query")})
	public JsonQueryObject<ConferenceStat> countConferencePercentageBynum(@ApiIgnore ConferenceStatQuery query,
			HttpServletRequest request, Model model) throws Exception {
		if (!PropertyUtil.objectNotEmpty(query.getSearchBeginDate())) {
			//取得当前年
			int year=DateUtil.getYear(new Date());
			query.setSearchBeginDate(year+"-01");
			query.setSearchEndDate(year+"-12");
		}
		List<ConferenceStat> list=conferenceStatService.countConferencePercentageBynum(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	@GetMapping("/countConferenceFinishOrg")
	@ApiOperation(value = "子级已完成指标的机构", notes = "已完成指标的机构")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchType", value = "会议类型", paramType = "query"),
			@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
			@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "query"),
			@ApiImplicitParam(name = "serarchNextNum", value = "子机构数", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationType", value = "机构类型", paramType = "query")})
	public JsonQueryObject<ConferenceStat> countConferenceFinishOrg(@ApiIgnore ConferenceStatQuery query,
			HttpServletRequest request, Model model) throws Exception {
		if (!PropertyUtil.objectNotEmpty(query.getSearchBeginDate())) {
			//取得当前年
			int year=DateUtil.getYear(new Date());
			query.setSearchBeginDate(year+"-01");
			query.setSearchEndDate(year+"-12");
		}
		List<ConferenceStat> list=conferenceStatService.countConferenceFinishOrg(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/findConferencePercentageByOrg")
	@ApiOperation(value = "各类型会议完成数量机构详情", notes = "各类型会议完成数量机构详情")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchType", value = "会议类型", paramType = "query"),
			@ApiImplicitParam(name = "searchReachConferenceNum", value = "会议次数", paramType = "query"),
			@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
			@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationName", value = "机构名称", paramType = "query"),
			@ApiImplicitParam(name = "searchOrganizationID", value = "机构id", paramType = "query")})
	public JsonQueryObject<ConferenceStat> findConferencePercentageByOrg(@ApiIgnore ConferenceStatQuery query,
			HttpServletRequest request, Model model) throws Exception {
		if (!PropertyUtil.objectNotEmpty(query.getSearchBeginDate())) {
			//取得当前年
			int year=DateUtil.getYear(new Date());
			query.setSearchBeginDate(year+"-01");
			query.setSearchEndDate(year+"-12");
		}
		List<ConferenceStat> list=conferenceStatService.findConferencePercentageByOrg(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
}
